/*
 * RangeData.fx
 */

package org.frapwidgets.scene.control.data;

import java.lang.IllegalArgumentException;

/**
 * @author Jonathan Merritt
 */

public class RangeData {

  public var min: Number = 0 on replace { checkMin(); }
  public var max: Number = 100 on replace { checkMax(); }
  public var value: Number = 0 on replace { checkValueAndExtent(); }
  public var extent: Number = 10 on replace { checkValueAndExtent(); }
  public var minExtent: Number = 0 on replace { checkValueAndExtent(); }

  function checkMin(): Void {
    if (min > max) {
      min = max;
    }
    checkValueAndExtent();
  }

  function checkMax(): Void {
    if (max < min) {
      max = min;
    }
    checkValueAndExtent();
  }

  function checkValueAndExtent(): Void {
    if (extent < minExtent) {
      extent = minExtent;
    }
    if ((value + extent) > max) {
      value = max - extent;
    }
    if (value < min) {
      value = min;
      if (extent > (max-min)) {
        extent = max - min;
        if (extent < minExtent) {
          def msg = "(max - min) < minExtent!";
          throw new IllegalArgumentException(msg);
        }
      }
    }
  }

}
